<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="default" :inline="true" label-width="68px">
      <el-form-item label="场所编号" prop="siteNumber">
        <el-input
          v-model="queryParams.siteNumber"
          placeholder="请输入场所编号"
          clearable
        />
      </el-form-item>
      <el-form-item label="场所名称" prop="siteName">
        <el-input
          v-model="queryParams.siteName"
          placeholder="请输入场所名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="场所地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入场所地址"
          clearable
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-perms="['sources:identification_sites:add']"
          v-preventReClick
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="identification_sitesList">
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="场所编号" align="center" prop="siteNumber" />
      <el-table-column label="场所名称" align="center" prop="siteName" />
      <el-table-column label="场所地址" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <status-tag :status-enum="SiteStatus" :status="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            type="primary"
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-perms="['sources:identification_sites:edit']"
            v-preventReClick
          >修改</el-button>
          <el-button
            type="primary"
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-perms="['sources:identification_sites:remove']"
            v-preventReClick
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-if="total>0"
      v-model:current-page="queryParams.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total"
      style="float: right"
    />

    <!-- 添加或修改鉴定场所对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="场所编号" prop="siteNumber">
          <el-input v-model="form.siteNumber" placeholder="请输入场所编号" />
        </el-form-item>
        <el-form-item label="场所名称" prop="siteName">
          <el-input v-model="form.siteName" placeholder="请输入场所名称" />
        </el-form-item>
        <el-form-item label="场所地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入场所地址" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in convertToOptions(SiteStatus)" :key="item.value" size="large"
                      :value="item.value.toString()">{{ item.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { IdentificationSitesService } from '@/api/sources/identification_sites.js'
import { convertToOptions } from '@/utils/util.js'
import { SiteStatus } from '@/enum.js'
import StatusTag from '@/components/StatusTag.vue'

export default {
  name: "Identification_sites",
  components: { StatusTag },
  computed: {
    SiteStatus() {
      return SiteStatus
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 鉴定场所表格数据
      identification_sitesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        siteNumber: null,
        siteName: null,
        address: null,
        contactPerson: null,
        contactPhone: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        siteNumber: [
          { required: true, message: "场所编号不能为空", trigger: "blur" }
        ],
        siteName: [
          { required: true, message: "场所名称不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "场所地址不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    convertToOptions,
    /** 查询鉴定场所列表 */
    getList() {
      this.loading = true;
      IdentificationSitesService.listIdentification_sites(this.queryParams).then(response => {
        this.identification_sitesList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        siteNumber: null,
        siteName: null,
        address: null,
        contactPerson: null,
        contactPhone: null,
        remark: null,
        createTime: null,
        updateTime: null,
        status:null
      };
      this.$nextTick(()=>{
        this.$refs.form.resetFields()
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields()
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "添加鉴定场所";
      this.reset();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      IdentificationSitesService.getIdentification_sites(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改鉴定场所";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            IdentificationSitesService.updateIdentification_sites(this.form).then(response => {
              this.$message.success("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            IdentificationSitesService.addIdentification_sites(this.form).then(response => {
              this.$message.success("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除鉴定场所编号为"' + ids + '"的数据项？').then(function() {
        return IdentificationSitesService.delIdentification_sites(ids);
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    },
  }
};
</script>
